import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import App from './App.vue'
import router from './router/index.js'
import './style.css'
import MapComponents from './components/map'

const app = createApp(App)

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

// 使用插件
app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
    locale: zhCn
})

// 注册地图组件
app.use(MapComponents)

// 挂载应用
app.mount('#app')

// 阻止页面缩放的函数
function preventZoom(e) {
    // 检测到多点触摸（用于捏合缩放）
    if (e.touches && e.touches.length > 1) {
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
}

// 双击可能导致缩放
function preventDoubleTapZoom(e) {
    const now = Date.now();
    if (now - lastTouchEnd <= 300) {
        e.preventDefault();
        e.stopPropagation();
    }
    lastTouchEnd = now;
}

// 应用阻止缩放的行为
let lastTouchEnd = 0;
document.addEventListener('touchstart', preventZoom, { passive: false });
document.addEventListener('touchmove', preventZoom, { passive: false });
document.addEventListener('touchend', preventDoubleTapZoom, { passive: false });

// 阻止键盘的缩放快捷键
document.addEventListener('keydown', function (e) {
    // 检测 Ctrl/Cmd + '+'/'-' 或 Ctrl/Cmd + 滚轮事件
    if ((e.ctrlKey || e.metaKey) && (e.key === '+' || e.key === '-' || e.key === '=' || e.key === '_')) {
        e.preventDefault();
        return false;
    }
});

// 阻止鼠标滚轮缩放
document.addEventListener('wheel', function (e) {
    if (e.ctrlKey || e.metaKey) {
        e.preventDefault();
        return false;
    }
}, { passive: false });
